
from dateutil import parser
from datetime import datetime

from models.exception import ParkAdminErrorCode, ParkAdminException

def Timedelta2Hours(timedelta):
    return round(timedelta.days * 24 + timedelta.seconds / 3600, 2) 

def Str2Date(dateStr : str):
    if not dateStr:
        raise ParkAdminException(ParkAdminErrorCode.InvalidParam)
    return parser.parse(dateStr)

def Date2Str(date : datetime):
    if not date:
        raise ParkAdminException(ParkAdminErrorCode.InvalidParam)
    return date.strftime("%Y-%m-%d %H:%M:%S")

def DatabaseDateTimeFormat(dateStr : str):
    return Str2Date(dateStr).strftime("%Y-%m-%d %H:%M:%S")
    
def ClientDateTimeFormat(dateStr : str):
    return Str2Date(dateStr).strftime("%Y-%m-%d")